package com.wefans.wefansapp.common.tool.popup;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wefans.wefansapp.R;
import com.wefans.wefansapp.common.tool.data.MFDataManager;
import com.wefans.wefansapp.common.tool.data.ResDailyData;
import com.wefans.wefansapp.common.tool.data.ResDailyTodayData;
import com.wefans.wefansapp.common.tool.data.ResDefaultData;
import com.wefans.wefansapp.common.tool.debug.MFDebug;
import com.wefans.wefansapp.common.tool.network.MFNetworkManager;
import com.wefans.wefansapp.common.tool.network.MFNetworkManager.IOnNetworkListener;
import com.wefans.wefansapp.common.tool.network.model.NetJsonData;
import com.wefans.wefansapp.common.tool.network.model.NetMessage;
import com.wefans.wefansapp.common.tool.utils.MFToastUtil;

public class DailyDialog extends BaseDialog {

	private int mCheckDay = -1;
	private String mStrBonusType = "";
	private Button mBtnClose = null;
	private TextView mTvDailyTitle = null;
	private ImageView[] mIvStemp = new ImageView[7];
	private Button mBtnDailyCheck = null;
	
	private OnDailyListener mDailyListener = null;
	
	public interface OnDailyListener {
		void onCheckDaily(boolean bSuccess);		
	}
	
	public DailyDialog(Context mContext, OnDailyListener dailyListener) {
		super(mContext);
		
		setContentView(R.layout.dialog_daily);
		
		mBtnClose = (Button)findViewById(R.id.BTN_DIALOG_CLOSE);
		mTvDailyTitle = (TextView)findViewById(R.id.TV_DAILY_TITLE);
		mBtnDailyCheck = (Button)findViewById(R.id.BTN_DAILY_CHECK);
		mIvStemp[0] = (ImageView)findViewById(R.id.IV_STEMP1);
		mIvStemp[1] = (ImageView)findViewById(R.id.IV_STEMP2);
		mIvStemp[2] = (ImageView)findViewById(R.id.IV_STEMP3);
		mIvStemp[3] = (ImageView)findViewById(R.id.IV_STEMP4);
		mIvStemp[4] = (ImageView)findViewById(R.id.IV_STEMP5);
		mIvStemp[5] = (ImageView)findViewById(R.id.IV_STEMP6);
		mIvStemp[6] = (ImageView)findViewById(R.id.IV_STEMP7);
		
		SimpleDateFormat sdf = new SimpleDateFormat("MM.dd");
		String currentDate = sdf.format(new Date());
		
		mTvDailyTitle.setText(currentDate);
		
		mDailyListener = dailyListener;
		mBtnClose.setOnClickListener(onClickListener);
		mBtnDailyCheck.setOnClickListener(onClickListener);
		
		requestDailyInfo();
	}
	
	private void requestDailyInfo() {
    	String strUrl = MFNetworkManager.BASE_URL + "/my/get_attend.php";
		strUrl = strUrl + "?mem_idx=" + MFDataManager.getInstance(getContext()).getMebmerIdx();
		MFNetworkManager.getInstance(getContext()).requestGetData(MFNetworkManager.REQUEST_DAILY_INFO, strUrl, mNetworkListener);
    }
	
	private void requestDailyCheck() {
    	String strUrl = MFNetworkManager.BASE_URL + "/my/set_attend.php";
		
    	Hashtable<String, String> body = new Hashtable<String, String>();
		body.put("mem_idx", MFDataManager.getInstance(getContext()).getMebmerIdx());
		body.put("cnt", String.valueOf(mCheckDay++));
		body.put("type", mStrBonusType);
    	
		MFNetworkManager.getInstance(getContext()).requestPostData(MFNetworkManager.REQUEST_DAILY_CHECK, strUrl, body, mNetworkListener);
    }
	
	private void updateDailyInfo(ResDailyData dtDailyData) {
		
		try {
			ResDailyTodayData dtToday = dtDailyData.getToday_attend();
    		
			if (dtToday != null) {
				mStrBonusType = dtToday.getType();
				
				String strContinueCount = dtToday.getContinuous();
    			int nContinueCount = Integer.parseInt(strContinueCount);
    			
    			if (nContinueCount > mIvStemp.length) {
    				
    			}
    			
    			mBtnDailyCheck.setText(String.format(getContext().getString(R.string.daily_dialog_check), dtToday.getNum()));
    			
    			mCheckDay = nContinueCount;
    			
    			for (int i = 0; i < nContinueCount; i++) {
    				mIvStemp[i].setVisibility(View.VISIBLE);
    			}
    		}
			
		} catch (Exception e) {
			MFDebug.e(e);
		}
	}
	
	IOnNetworkListener mNetworkListener = new IOnNetworkListener() {
		
		@Override
		public void onNetMessage(int resultCode, NetMessage message) {
			try {
	            //서버 여동 데이터
	            if( message instanceof NetJsonData ) {
	                long magicNum = ((NetJsonData) message).getMagicNum();
	                String json = ((NetJsonData) message).getResponseJson();
	                
	                if( json == null ) {
	                    throw new Exception("invalid json data");
	                }
	                
	                Gson gson = new Gson();
	                
	                if (magicNum == MFNetworkManager.REQUEST_DAILY_INFO) {
	                	
	                	ResDailyData resultData = (ResDailyData)gson.fromJson(json, ResDailyData.class);
	                    if( resultData == null ) {
	                        throw new Exception("Gson parsing error");
	                    }
	                    
	                	if (resultData.isSuccsess()) {
	                		updateDailyInfo(resultData);
	                		
	                	} else {
	                		MFToastUtil.NetResponseToast(getContext(), resultData.getResultcode(), resultData.getResultmsg());
	                	}
	                } else if (magicNum == MFNetworkManager.REQUEST_DAILY_CHECK) {
	                	ResDefaultData resultData = (ResDefaultData)gson.fromJson(json, ResDefaultData.class);
	                    if( resultData == null ) {
	                        throw new Exception("Gson parsing error");
	                    }
	                	if (resultData.isSuccsess()) {
	                		if (mDailyListener != null) {
	        					mDailyListener.onCheckDaily(true);
	        				}
	        				dismiss();
	                	} else {
	                		MFToastUtil.NetResponseToast(getContext(), resultData.getResultcode(), resultData.getResultmsg());
	                	}
	                }
	            }	            
	        } catch(Exception e) {
	            MFDebug.e(e);
	        }		
		}
	};

	android.view.View.OnClickListener onClickListener = new android.view.View.OnClickListener() {
		
		public void onClick(View v) {
			
			switch (v.getId()) {
			case R.id.BTN_DAILY_CHECK:
				requestDailyCheck();
				break;
			case R.id.BTN_DIALOG_CLOSE:
				dismiss();
				break;
			}
		}
	};

}
